package cn.xej.health.team.service.impl;

import cn.xej.health.common.utils.CommonResult;
import cn.xej.health.common.utils.PageUtils;
import cn.xej.health.common.utils.Query;
import cn.xej.health.team.dao.DivisionDao;
import cn.xej.health.team.dao.TeamDao;
import cn.xej.health.team.entity.Division;
import cn.xej.health.team.entity.Team;
import cn.xej.health.team.service.TeamService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xej
 */

@Service
public class TeamServiceImpl extends ServiceImpl<TeamDao, Team> implements TeamService {

    @Autowired
    private TeamDao teamDao;

    @Autowired
    private DivisionDao divisionDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Team> page = this.page(new Query<Team>().getPage(params), new QueryWrapper<Team>().orderByAsc("tid"));
        return new PageUtils(page);
    }

    @Override
    public CommonResult queryTeamList(Integer pageIndex, Integer pageSize, String searchName) {
        Map<String,Object> map = new HashMap<>();
        int startNum = (pageIndex-1)*pageSize+1;
        int endNum = pageIndex*pageSize;
        List<Team> teamList = teamDao.queryTeamList(startNum,endNum,searchName);

        /**
         * 将teamList中的team根据did，递归赋给dname
         */
        List<Division> divisionList = divisionDao.selectList(null);
        teamList = teamList.stream().map((team) -> {
            Division division = divisionDao.selectById(team.getDid());
            LinkedList<String> list = new LinkedList<>();
            getParentDivision(divisionList,list,division);
            team.setDname(getDivisionStr(list));
            return team;
        }).collect(Collectors.toList());


        Integer count = teamDao.selectCount(new QueryWrapper<Team>().like("tname",searchName));

        map.put("data",teamList);
        map.put("total",count);

        return CommonResult.success(map);
    }

    /**
     * 获取所有区划
     * @return
     */
    @Override
    public CommonResult queryDivisionList() {
        List<Division> divisionList = divisionDao.selectList(null);
        return CommonResult.success(divisionList);
    }

    private String getDivisionStr(LinkedList<String> list) {
        StringBuffer sb = new StringBuffer();
        for (int i=0;i<list.size()-1;i++){
            sb.append(list.get(i));
            sb.append(" --》 ");
        }
        sb.append(list.get(list.size()-1));
        return sb.toString();
    }

    private void getParentDivision(List<Division> divisionList, LinkedList<String> list, Division child) {
        list.addFirst(child.getDname());
        if(child.getPid() != 0){
            for (Division division : divisionList) {
                if (child.getPid().equals(division.getDid())){
                    getParentDivision(divisionList,list,division);
                }
            }
        }
    }

    /**
     * 获取团队树
     * @param did
     * @return
     */
    @Override
    public CommonResult queryTeamTree(Integer did) {
        List<Team> teamTreeList = teamDao.queryTeamTree(did);

        return CommonResult.success(teamTreeList);
    }
}
